<?php

namespace App\Http\Controllers\Api;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Sms;

class ApiController extends Controller
{

    use ApiResponse;

    public function checkSmsCode($phone, $sms_code)
    {
        $sms = Sms::where(['phone' => $phone, 'code' => $sms_code])->orderBy('id', 'desc')->first();

        if (!$sms) {
            return ['message' => '短信验证码不存在'];
        }
        if ($sms->status == 1) {
            return ['message' => '短信验证码已被使用'];
        }
        if ($sms->expire_time < time()) {
            return ['message' => '短信验证码已过期'];
        }
        if ($sms_code !== $sms->code) {
            return ['message' => '短信验证码错误'];
        }
        $sms->status = 1;
        $sms->save();
        return true;
    }

}
